package com.ll.gulimall.product.web;

import com.alibaba.nacos.common.util.UuidUtils;
import com.ll.gulimall.product.entity.CategoryEntity;
import com.ll.gulimall.product.service.CategoryService;
import com.ll.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询一级分类
     *
     * @param model
     * @return
     */
    @GetMapping({"/", "/index.html"})
    public String index(Model model) {

        List<CategoryEntity> entities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", entities);
        return "index";
    }

    /**
     * 分装子分类数据      //index/catalog.json
     */
    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogs() {

        Map<String, List<Catelog2Vo>> catalogs = categoryService.getCatalogs();

        return catalogs;
    }

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/test")
    @ResponseBody
    public String testLock() {
        //得到一把锁
        RLock lock = redissonClient.getLock("my-lock");
        //上锁  阻塞式等待 默认加的锁都是30s时间。
        // 如果业务超长，运行期间自动给锁续上新的30s。不用担心业务时间长，锁自动过期被删掉
        // 加锁的业务只要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认在30s以后自动删除。
        lock.lock();
        //执行锁的业务
        try {
            Thread.sleep(30000);
            System.out.println("模拟执行业务");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //释放锁
        lock.unlock();
        System.out.println("释放锁");
        return "hello";
    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/write")
    @ResponseBody
    public String writeValue() {
        String s = "";
        // 获得读写锁对象
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("wr-lock");
        // 添加写锁
        RLock rLock = readWriteLock.writeLock();
        // 上锁
        rLock.lock();
        try {
            s = UuidUtils.generateUuid();
            redisTemplate.opsForValue().set("wrkey", s);
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue() {
        // 获得读写锁对象
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("wr-lock");
        // 添加读锁
        RLock rLock = readWriteLock.readLock();
        // 上锁
        rLock.lock();
        String s = "";
        try {
            s = redisTemplate.opsForValue().get("wrkey");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/fangxue")
    @ResponseBody
    public String fangxue() {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);//闭锁条件 需要走5次
        try {
            door.await();//闭锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "放学了";
    }

    @GetMapping("/gogo")
    @ResponseBody
    public String gogo() {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();//计数-1
        return "放学了";
    }

    @GetMapping("/tingche")
    @ResponseBody
    public String tingche() throws InterruptedException {
        RSemaphore xinhao = redissonClient.getSemaphore("xinhao");
        xinhao.acquire();//得到一个值
        xinhao.tryAcquire();//看看是否能获取一个值
        return "停车";
    }


    @GetMapping("/go")
    @ResponseBody
    public String go(){
        RSemaphore xinhao = redissonClient.getSemaphore("xinhao");
        xinhao.release();//释放一个值
        return "开走了";
    }
}
